from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import json

class base():
    def __init__(self,driver):
        self.driver = driver


    def get_url(self,url):
        self.driver.get(url)# 网址
    def get_ele(self,*ele):
        return self.driver.find_element(*ele)# 元素定位

    def get_text(self,text,*ele):
        self.get_ele(*ele).send_keys(text)# 输入内容

    def get_click(self,*ele):
        self.get_ele(*ele).click()# 点击

    def get_move(self,*ele):
        aa = self.get_ele(*ele)
        ActionChains(self.driver).move_to_element(aa).perform()# 悬停

    def get_qx(self,*ele):
        self.get_ele(*ele).send_keys(Keys.CONTROL, 'a')# 全选

    def get_sc(self,*ele):
        self.get_ele(*ele).send_keys(Keys.DELETE)# 删除

    def get_fz(self,*ele):
        self.get_ele(*ele).send_keys(Keys.CONTROL, 'c')#复制

    def get_zt(self,*ele):
        self.get_ele(*ele).send_keys(Keys.CONTROL, 'v')# 粘贴

    def get_huiche(self,*ele):
        self.get_ele(*ele).send_keys(Keys.ENTER)# 回车

    def get_sleep(self,c=3):
        sleep(c)

    def get_wait(self):
        self.driver.implicitly_wait(5)# 显示等待

    def get_time(self,*ele):
        WebDriverWait(self.driver, 8, 0.5).until(EC.visibility_of_element_located(*ele)) # 隐式等待

    def get_hold(self,*ele):
        ad = self.get_ele(*ele)
        ActionChains(self.driver).click_and_hold(ad).perform()# 拖住不放

    def get_frame(self,*ele):
        tu = self.get_ele(*ele)
        self.driver.switch_to.frame(tu)# Iframe
        self.get_sleep()

    def get_max(self):
        self.driver.maximize_window()  # 窗口最大化

    def get_jb(self,c):
        at = self.driver.window_handles
        self.driver.switch_to.window(at[c])# 切换窗口

    def get_gun(self):
        self.driver.execute_script("window.scrollTo(0,500)")# 滚动

    def store_cookie(self):
        cookie = self.driver.get_cookies()
        file = open('cookie','w',encoding='utf-8')# 储存cookie
        file.write(json.dumps(cookie))
        file.close()

    def add_cookie(self):
        self.driver.delete_all_cookies()
        file = open('cookie','r')
        cookie = json.loads(file.read()) # 读取cookie
        for i in cookie:
            self.driver.add_cookie(i)
        file.close()

    def get_sx(self):
        self.driver.refresh() # 刷新



    def get_mini(self):
        self.driver.minimize_window()# 窗口最小化